<?php

namespace Api\Jobs;

use Api\Http\Controllers\WechatController;
use Api\Service\YunyangService;
use Carbon\Carbon;
use Clockwork\Request\Log;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\Member\Models\Members;
use Modules\Order\Models\Orders;
use Illuminate\Support\Facades\Log as Logs;

class AddPostmanInfo implements ShouldQueue
{
    use Queueable, Dispatchable, InteractsWithQueue, SerializesModels;

    public $orderId;
    public $second;

    /**
     * Create a new job instance.
     */
    public function __construct($orderId,$second)
    {
        $this->orderId = $orderId;
        $this->second = $second;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {

        $orderInfo = Orders::find($this->orderId);
        $waybill = $orderInfo->waybill;

        $data['waybill'] = $waybill;
        $data['traceFormat'] = 'obj';
        $json = (new YunyangService())->getRoute($data);
        $result = json_decode($json, true);
        $comment = current($result['result']);
        $this->second += 3;
        if(!empty($comment)){
            $comment = $comment['comments'];

            if(empty($comment) && $this->second <120){

                AddPostmanInfo::dispatch($orderInfo->id,$this->second)->delay(Carbon::now()->addSeconds($this->second));
            }else{
                $numbers = preg_replace('/[^0-9]/', '', $comment);
                $postman_mobile = substr($numbers,0,11);
                $pattern = '/快递员姓名:([\x{4e00}-\x{9fa5}]+)/u';
// 使用 preg_match 进行匹配
                if (preg_match($pattern, $comment, $matches)) {
                    // $matches[1] 将包含匹配到的姓名
                    $courierName = $matches[1];
                }else{
                    $courierName= '快递员';
                }
                $orderInfo->postman_mobile = $postman_mobile;
                $orderInfo->postman_name = $courierName;
                $orderInfo->save();
                //订单推送
                $userInfo = Members::find($orderInfo->user_id);
                $message = [];
                $message['character_string6'] = ['value'=>$orderInfo->waybill];
                $message['thing7'] = ['value'=>$orderInfo->tag_type];
                $message['thing11'] = ['value'=>$courierName];
                $message['phone_number5'] = ['value'=>$postman_mobile];
                $message['thing3'] = ['value'=>'速速寄联盟，低至5元发全国'];
                $obj = (object)$message;
                $wechat = new WechatController();
                $temp = ['tag_type'=>$orderInfo->tag_type,'channel_logo_url'=>$orderInfo->channel_logo_url];
                $tempObj = (object)$temp;
                $json = json_encode($tempObj);
                $page = '/pages/order/logistics?waybill='.$orderInfo->waybill.'&channelDic='.$json;
                $wechat->sendMessage('yFvI_YER4KQmBmkPxcOf6Gpygz8GzPZ7lW9ADsfo1cc',$userInfo->open_id,$obj,$page);
            }
        }else{
            if(empty($comment) && $this->second <120){

                AddPostmanInfo::dispatch($orderInfo->id,$this->second)->delay(Carbon::now()->addSeconds($this->second));
            }
        }


    }
}
